package com.zj.healthmanage.controller;

import com.zj.healthmanage.constant.RedisConstant;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.entity.Result;
import com.zj.healthmanage.pojo.Setmeal;
import com.zj.healthmanage.service.SetmealService;
import com.zj.healthmanage.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/setmeal")
@Api(value = "套餐",description = "套餐")
public class SetMealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    @ApiOperation(value = "上传套餐图片",httpMethod = "POST")
    @RequestMapping("/uploadSetMealImage")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);
        String fileName = UUID.randomUUID().toString() + extention;
        try{
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"上传图片失败");
        }
        return new Result(true, "上传图片成功",fileName);
    }

    @ApiOperation(value = "新增套餐",httpMethod = "POST")
    @RequestMapping("/addSetMeal")
    public Result addSetMeal(@RequestBody Setmeal setmeal, Integer[] checkgroupIdList){
        try {
            setmealService.insertSelective(setmeal,checkgroupIdList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增套餐失败");
        }
        return new Result(true,"新增套餐成功");
    }

    @ApiOperation(value = "分页查询套餐",httpMethod = "POST")
    @RequestMapping("/querySetMealByPage")
    public PageResult querySetMealByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.querySetMealByPage(queryPageBean);
        return pageResult;
    }

    @ApiOperation(value = "查询单个套餐",httpMethod = "POST")
    @RequestMapping(value = "/querySetMealById")
    public Result querySetMealById( Integer id) {
        try {
            Setmeal setmeal = setmealService.selectByPrimaryKey(id);
            return new Result(true, "查询套餐成功",setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询套餐失败");
        }
    }

    @ApiOperation(value = "根据套餐id查包含的多个检查组id",httpMethod = "POST")
    @RequestMapping(value = "/queryCheckGroupIdsById")
    public List<Integer> queryCheckGroupIdsBySetmealId(Integer id){
        List<Integer> list = setmealService.queryCheckGroupIdsById(id);
        return list;
    }


    @ApiOperation(value = "编辑套餐",httpMethod = "POST")
    @RequestMapping(value = "/editSetMeal")
    public Result editSetMeal(@RequestBody Setmeal setmeal,Integer[] checkgroupIdList) {
        try {
            setmealService.updateByPrimaryKeySelective(setmeal,checkgroupIdList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑套餐失败");
        }
        return new Result(true, "编辑套餐成功");
    }

    @ApiOperation(value = "删除套餐",httpMethod = "POST")
    @RequestMapping(value = "/deleteSetMealById")
    public Result deleteSetMealById(Integer id) {
        try {
            setmealService.deleteByPrimaryKey(id);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除套餐失败");
        }
        return new Result(true, "删除套餐成功");
    }
}
